package com.turing.post.organization.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.MerchantBranch;
import com.turing.post.organization.util.MerchantBranchBusiness;
import com.turing.post.organization.util.MerchantBranchDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 商户分支机构业务接口
 * 
 * @author zxp
 * @Dec 20, 2012
 */
@Stateless(mappedName = "organization.impl.MerchantBranchBusinessImpl")
@Remote
@Local
public class MerchantBranchBusinessImpl implements MerchantBranchBusiness {

	@EJB(mappedName = "organization.impl.MerchantBranchDaoImpl")
	private MerchantBranchDao merchantBranchDaoImpl;

	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@Override
	public void insertMerchantBranchInfo(PCPSWebInput webInput,
			MerchantBranch merchantBranch) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 业务检查
		if (merchantBranch == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"待创建商户机构为空！");
		}
		// 编号
		if (PCPSUtil.isEmpty(merchantBranch.getMerchantBranchNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"商户机构编号为空！");
		}
		// 机构名称
		if (PCPSUtil.isEmpty(merchantBranch.getMerchantBranchName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"商户机构名称为空！");
		}
		// 名称
		// if (PCPSUtil.isEmpty(merchantBranch.getRegionNo())) {
		// throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
		// "行政区域为空!");
		// }
		// 验证机构号是否已经使用
		if (merchantBranchDaoImpl
				.getMerchantBranchByMerchantBranchNo(merchantBranch
						.getMerchantBranchNo()) != null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.BRANCHNO_EXIST, "商户机构号已经存在！");
		}
		// 商户分支机构机构归属商户2013-04-24修正
		if (!PCPSUtil.isEmpty(merchantBranch.getSuperMerchantBranchNo())) {
			MerchantBranch mb = merchantBranchDaoImpl
					.getMerchantBranchByMerchantBranchNo(merchantBranch
							.getSuperMerchantBranchNo());
			merchantBranch.setMerchantNo(mb.getMerchantNo());
			merchantBranch.setRegionNo(mb.getRegionNo());
		}

		// 插入平台机构主键表数据
		SysBranch sysBranch = sysBranchDaoImpl
				.getSysBranchByBranchNo(merchantBranch.getMerchantBranchNo());
		if (sysBranch == null) {
			sysBranch = new SysBranch(merchantBranch.getMerchantBranchNo(),
					SysBranch.SYSBRANCH_MERCHANTBRANCH);
			sysBranchDaoImpl.insertSysBranch(sysBranch);
		}

		// 插入发卡商分支机构
		merchantBranchDaoImpl.insertMerchantBranch(merchantBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "创建商户机构", merchantBranch,
				SysOperLog.operType_Add, "MerchantBranchName:"
						+ merchantBranch.getMerchantBranchName());
	}

	@Override
	public void updateMerchantBranchInfo(PCPSWebInput webInput,
			MerchantBranch merchantBranch) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		MerchantBranch _merchantBranch = merchantBranchDaoImpl
				.getMerchantBranchByMerchantBranchNo(merchantBranch
						.getMerchantBranchNo());
		// 业务检查
		if (_merchantBranch == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改商户分支机构数据库不存在!");
		}
		// 对象赋值
		_merchantBranch.setMerchantBranchName(merchantBranch
				.getMerchantBranchName());
		_merchantBranch.setAddress(merchantBranch.getAddress());
		_merchantBranch.setLinkMan(merchantBranch.getLinkMan());
		_merchantBranch.setLinkTele(merchantBranch.getLinkTele());
		// 插入发卡商分支机构
		merchantBranchDaoImpl.updateMerchantBranch(_merchantBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改商户机构", merchantBranch,
				SysOperLog.operType_Edit, "MerchantBranchName:"
						+ merchantBranch.getMerchantBranchName());
	}

	@Override
	public void deleteMerchantBranch(PCPSWebInput webInput, String mrchNo)
			throws PCPSApplicationException {

	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, MerchantBranch obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
